How to Calculate Cumulative Sum-Running Total

Let’s say we want to see a report with cumulative values, for example the cumulative daily revenue at different timestamps. We want the cumulative revenue at each timestamp in the table:

sales_ts amount
2018-01-01 00:10:10
2018-01-01 00:11:00
2018-01-01 00:11:20
2018-01-01 00:12:12
2018-01-01 00:12:00
select 	sales_ts,
        sum(amount) over (order by sales_ts) 
from sales

The previous query calculates the cumulative revenue considering only the records with timestamps previous to the current timestamp.

sales_ts amount
2018-01-01 00:10:10
2018-01-01 00:11:00
2018-01-01 00:11:20
2018-01-01 00:12:12
2018-01-01 00:12:00

IN THIS PAGE